package cac.fenjian.resource.mscarbasic.api;

import cac.fenjian.resource.MsCarBasic.dto.MsCarBasicDTO;
import cac.fenjian.resource.common.InvokeService;
import com.cac.dme.adapter.rest.client.RestClient;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.ResponseMsg;
import com.cac.dme.adapter.utils.RestClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Component
@RestController
@RequestMapping("/workapi/")
public class MsCarBasicApi {
    /**
     * 服务编码
     */
    private static final String SERVICE_CODE="xdCommonData";
    private static final String BASE_PATH = "/mscar/";

    @Autowired
    private RestClient restClient;
    @Autowired
    private InvokeService invokeService;
    /**
     * 按条件分页查询api
     * @param queryReqBean
     * @return
     */
    @PostMapping("/searchByPage")
    public String searchByPage(@RequestBody QueryReqBean<MsCarBasicDTO> queryReqBean ){
        String url = BASE_PATH + "/searchByPage/v1";
        String result = invokeService.callServicePost(url,SERVICE_CODE,queryReqBean);
        return  result;
    }
    /**
     * 根据条件查询数据
     * @param queryReqBean
     * @return
     */
    @PostMapping("/searchByParams")
    public String searchMsCarByParams(@RequestBody QueryReqBean<MsCarBasicDTO> queryReqBean){
        String url = BASE_PATH + "/searchByParams/v1";
        String result= invokeService.callServicePost(url,SERVICE_CODE,queryReqBean);
        return result;
    }
    @GetMapping("/getMsCar/{id}")
    public MsCarBasicDTO get(@PathVariable  String id){
        String url = BASE_PATH+"/get/v1/"+id;
        ResponseMsg<MsCarBasicDTO> responseMsg = restClient.doGet(SERVICE_CODE, url, new ParameterizedTypeReference<ResponseMsg<MsCarBasicDTO>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }
    /**
     * 新增功能API
     * @param msCarBasicDTO
     * @return
     */
    @PostMapping("/insertMsCar")
    public String insertMsCar(@RequestBody MsCarBasicDTO msCarBasicDTO){
        String url = BASE_PATH +"/save/v1";
        ResponseMsg<String> responseMsg = restClient.doPost(SERVICE_CODE, url, msCarBasicDTO, new ParameterizedTypeReference<ResponseMsg<String>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }
    /**
     * 批量新增API
     * @param msCarBasicDTOList
     * @return
     */
    @PostMapping("/insertManyMsCar")
    public Integer insertMsCarMany(@RequestBody List<MsCarBasicDTO> msCarBasicDTOList){
        String url = BASE_PATH +"/saveMany/v1";
        ResponseMsg<Integer> responseMsg = restClient.doPost(SERVICE_CODE, url, msCarBasicDTOList, new ParameterizedTypeReference<ResponseMsg<Integer>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }

    /**
     * 删除API
     * @param id
     * @return
     */
    @PostMapping("/deleteMsCar")
    public Integer deleteMsCar(@RequestBody String id){
        String url = BASE_PATH+"/deleteById/v1";
        ResponseMsg<Integer> responseMsg = restClient.doPost(SERVICE_CODE, url, id, new ParameterizedTypeReference<ResponseMsg<Integer>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }

    /**
     * 批量删除apI
     * @param id
     * @return
     */
    @PostMapping("/deleteManyMsCar")
    public Integer deleteManyMsCar(@RequestBody String[] id){
        String url = BASE_PATH+"/deleteByIds/v1";
        ResponseMsg<Integer> responseMsg = restClient.doPost(SERVICE_CODE, url, id, new ParameterizedTypeReference<ResponseMsg<Integer>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    @PostMapping("/updateMsCar")
    public Integer updateMsCar(@RequestBody MsCarBasicDTO msCarBasicDTO){
        String url = BASE_PATH+"/update/v1";
        ResponseMsg<Integer> responseMsg = restClient.doPost(SERVICE_CODE, url, msCarBasicDTO, new ParameterizedTypeReference<ResponseMsg<Integer>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }
}
